// pageMerchant/myPurchaseList/index.js
var app = getApp();
Page({

  data: {
    imgUrl:app.globalData.imgUrl,
    goodsList:[],
    totalPage: 0, // 总页数
    queryParams: {
      page: 1,
      size: 10,
    },
    pullFlag: false, // 是否向上拉动到底部
    listTip: '上拉加载更多'
  },
  onshow(){
    this.getList()
  },
  onReady(){
    this.getList()
  },
  // 获取订单详情
  getList(){
    this.setData({ listTip: '加载中...' })
    const { page, size } = this.data.queryParams
    wx.$req({
      url: `/wms/orderPurchase/list?page=${page - 1}&size=${size}`,
      method: 'post',
      data:{},
      loadingText: '加载中',
    }).then(res => {
      console.log(res)
      let totalPage = res.data.totalPages
      let rows = this.data.goodsList

      if (totalPage == 1 || this.data.totalPage === totalPage ) {
        this.setData({ listTip: '暂无更多' })
      } else {
        this.setData({ listTip: '上拉加载更多' })
      }

      if (totalPage == 0) {
        this.setData({ listTip: '暂无内容' })
      }
    
      // 列表数据：为true 则需要push, false，则直接覆盖
      if (this.data.pullFlag) {
        rows.push(...res.data.content)
      } else {
        rows = res.data.content
      }

      this.setData({ 
        goodsList: rows,
        totalPage
      })
    }).finally(() => { wx.stopPullDownRefresh() })
  },
/**
	* 页面相关事件处理函数--监听用户下拉动作
	*/
	onPullDownRefresh() {
		const queryParams = { page: 1,  size: 10 }

		this.setData({ 
			pullFlag: false,
			queryParams
		})

		this.getList()
	},
	
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    let { page, size } = this.data.queryParams

    if (page < this.data.totalPage) {

      page += 1
      this.setData({  
        pullFlag: true,
        queryParams: { page,  size },
      })
      this.getList()
    }
  },

  toInfo (e) {
    const info = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: "/pageMerchant/orderInfo/index?info="+info
    })
  }

})
